/**
 * role-menu edit
 */
let tree, form, layer, $;
layui.use(['iconPickerFa', 'form', 'tableSelect', 'tree', 'util'], function () {
    form = layui.form,
        layer = layui.layer,
        tree = layui.tree,
        $ = layui.$;
    let u = localStorage.getItem("ADMIN_LOGIN");
    let loginU = {};
    if (u == undefined) {
        layer.msg('请登录', {icon: 1, time: 500}, function () {
            window.location = 'page/login.html';
        });
    } else {
        loginU = decryptLocalStorage(u);
    }
    let _data = localStorage.getItem("roleData");
    let viewData = undefined;
    let roleId = 0;
    if (_data != undefined) {
        viewData = decryptLocalStorage(_data, loginU.token);
        roleId = viewData.roleId;
        // $("#roleId").val(viewData.roleId);
        $("#roleName").val(viewData.roleName);
        if (viewData.roleName == '超级管理员') {
            $("#roleName").attr("disabled", "disabled");
        }
    }
    //tree
    request.otherGetMethod('roleMenu', 'tree', {token: loginU.token, roleId: roleId}, function (result) {
        if (result.code == 0) {
            //常规用法
            tree.render({
                elem: '#menuTree' //默认是点击节点可进行收缩
                , data: result.data.treeData
                , id: 'menuTree'
                , showCheckbox: true //是否显示复选框
                , showLine: true //是否开启连接线
                , click: function (obj) {
                    var data = obj.data; //获取当前点击的节点数据
                    //layer.msg('状态：'+ obj.state + '<br>节点数据：' + JSON.stringify(data));
                }
            });
            var _data = result.data.checkedArray;
            tree.setChecked('menuTree', _data);
        } else {
            layer.msg('保存失败', {
                icon: 2,
                time: 2000
            });
        }
    });

    function getCheckedId(data) {
        var id = "";
        $.each(data, function (index, item) {
            if (id != "") {
                id = id + "," + item.id;
            } else {
                id = item.id;
            }
            //item 没有children属性
            if (item.children != null) {
                var i = getCheckedId(item.children);
                if (i != "") {
                    id = id + "," + i;
                }
            }
        });
        return id;
    }


    //监听提交
    form.on('submit(saveBtn)', function (data) {
        let params=data.field;
        let checkedData = tree.getChecked('menuTree');//获取选中节点的数据
        if (checkedData.length == 0) {
            layer.msg('请选择菜单', {
                icon: 2,
                time: 2000
            });
            return;
        }
        let roleName = $("#roleName").val();
        if (roleName == '') {
            layer.msg('请输入角色名称', {
                icon: 2,
                time: 2000
            });
            return;
        }
        params.token = loginU.token;
        params.menuIds = getCheckedId(checkedData);
        params.roleId = roleId;
        params.roleName = roleName;
        //添加
        let _loading = layer.load(2, {shade: [0.5, '#fff']});
        request.otherGetMethod('roleMenu', 'batchSetMenu', params, function (result) {
            if (_loading != undefined) {
                layer.close(_loading);
            }
            if (result.code == 0) {
                layer.msg('保存成功', {
                    icon: 1,
                    time: 500
                }, function () {
                    // 关闭弹出层
                    var iframeIndex = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(iframeIndex);
                });

            } else {
                layer.msg('保存失败', {
                    icon: 2,
                    time: 2000
                });
            }
        });

    });
});
